package com.njdj.gps.web.controller;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.dom4j.DocumentException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.njdj.gps.common.constants.NjdjConstants;
import com.njdj.gps.common.page.PageList;
import com.njdj.gps.common.page.PageProperty;
import com.njdj.gps.common.util.Utils;
import com.njdj.gps.kernel.entity.GpsCarHis;
import com.njdj.gps.kernel.entity.SeDevice;
import com.njdj.gps.kernel.entity.SeTask;
import com.njdj.gps.kernel.service.IGpsCarHisService;
import com.njdj.gps.kernel.service.ISeDeviceService;
import com.njdj.gps.kernel.service.ISeTaskService;



@Controller
public class CarHisController extends BaseController {
	
	@Autowired
	IGpsCarHisService gpsCarHisService;
	@Autowired
	ISeDeviceService seDeviceService;
	
	@RequestMapping("/carhis.do")
	public ModelAndView loginHandler(HttpServletRequest request, HttpServletResponse response, ModelMap model) throws ServletException, DocumentException {
		
		//获取carids
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("status", 0);
		if(null != this.getRole(request) && "administrator".equals(this.getRole(request))){}
		else
			param.put("onwer", this.getUserName(request));
		List<SeDevice> deviceList = seDeviceService.listModel(param);
		model.put("deviceList", deviceList);
		
		//检索参数
		param.clear();
		if(null != deviceList && deviceList.size() > 0){
			String carid = this.getParameter(request, "carid");
			if(null != carid && !"".equals(carid)){
				param.put("carid", carid);
				model.put("carid", carid);
			} else {
				param.put("carid", deviceList.get(0).getCarid());
				model.put("carid", deviceList.get(0).getCarid());
			}
		} else {
			return null;
		}
		Date date = new Date();  //默认时间
		String beginDate = this.getParameter(request, "beginDate");
		if(!"".equals(beginDate)){
			param.put("beginDate", beginDate);
			model.put("beginDate", beginDate);
		} else {
			SimpleDateFormat format = new SimpleDateFormat("yyyy-M-dd"); // 2012-12-02 04:38:06
			param.put("beginDate", format.format(date));
			model.put("beginDate", format.format(date));
		}
		String endDate = this.getParameter(request, "endDate");
		if(!"".equals(endDate)) {
			param.put("endDate", endDate);
			model.put("endDate", endDate);
		} else {
			SimpleDateFormat format = new SimpleDateFormat("yyyy-M-dd"); // 2012-12-02 04:38:06
			param.put("endDate", format.format(date));
			model.put("endDate", format.format(date));
		}
		List<GpsCarHis> gpsCarHisList = gpsCarHisService.listCarHis(param);
		
		model.put("gpsCarHisList", gpsCarHisList);
		
		if(null != gpsCarHisList && gpsCarHisList.size() > 0){
			GpsCarHis startPoint = gpsCarHisList.get(0);
			GpsCarHis endPoint = gpsCarHisList.get(gpsCarHisList.size()-1);
			
			model.put("startPoint", startPoint);
			model.put("endPoint", endPoint);
		}
		
		return new ModelAndView("carhis/carhis", model);
	}

}
